﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Client_Server_Interface;

namespace BTM_Client_Application.SupportClasses
{
    public class StationElement
    {

        public StationElement()
        {
            Devices = new List<IDeviceInterface>();
        }

        public DeviceTreeItem.StationInfo Info { get; set; }

        public UIElement Marker { get; set; }

        public TreeViewItem TreeItem { get; set; }

        public Popup StationPopup { get; set; }

        public MapMarker MapMarker
        {
            get { return _mapMarker; }
            set
            {
                _mapMarker = value;
                Marker = value.GetMarker();
            }
        }

        public List<IDeviceInterface> Devices { get; set; }

        private MapMarker _mapMarker;

        public static UseDispatcherDelegate UseMainWindowDispatcherProperty { get; set; }

        private static void UseMainWindowDispatcher(VoidDelegate del)
        {
            if (UseMainWindowDispatcherProperty != null)
                UseMainWindowDispatcherProperty(del);
        }

        public void DeviceErrorsListChanged(object sender, EventArgs e)
        {
            var device = sender as IDeviceInterface;
            
            if (device.UnreadCount > 0)
            {
                _mapMarker.Background = Res.ErrorBrush;
                SetTreeItemState(ErrorsState.HaveUnseen);
            }
            else
            {
                if (device.ErrorsList.Count > 0)
                {
                    _mapMarker.Background = Res.HaveSeenErrorsMapBrush;
                    SetTreeItemState(ErrorsState.HaveSeen);
                    
                }
                else
                {
                    _mapMarker.Background = Res.OkMapMarkerBrush;
                    SetTreeItemState(ErrorsState.NoErrors);
                }
            }
            
        }

        private void SetTreeItemState(ErrorsState state)
        {
            string itemName = "";
            var parentStatesList = new Dictionary<string, ErrorsState>();
            UseMainWindowDispatcher(() =>
                {
                    itemName = TreeItem.Header.ToString();
                    parentStatesList = (TreeItem.Parent as TreeViewItem).Tag as Dictionary<string, ErrorsState>;
                });
            
            switch (state)
            {
                case ErrorsState.HaveSeen:
                    UseMainWindowDispatcher(() =>
                        {
                            //(TreeItem.Parent as TreeViewItem).Background = Res.HaveSeenErrorsBrush;
                            parentStatesList[itemName] = state;
                            TreeItem.Background = Res.HaveSeenErrorsBrush;
                        });
                    break;
                case ErrorsState.HaveUnseen:
                    UseMainWindowDispatcher(() =>
                        {
                            //(TreeItem.Parent as TreeViewItem).Background = Res.ErrorBrush;
                            parentStatesList[itemName] = state;
                            TreeItem.Background = Res.ErrorBrush;
                        });
                    break;
                case ErrorsState.NoErrors:
                    UseMainWindowDispatcher(() =>
                        {
                            if (parentStatesList.ContainsKey(itemName))
                            {
                                parentStatesList.Remove(itemName);
                            }
                            //(TreeItem.Parent as TreeViewItem).Background = Res.OkBrush;
                            TreeItem.Background = Res.OkBrush;
                        });
                    break;
            }
            UpdateParentTreeItemState();
        }

        private void UpdateParentTreeItemState()
        {
            UseMainWindowDispatcher(() =>
                {
                    var parent = TreeItem.Parent as TreeViewItem;
                    var statesList = parent.Tag as Dictionary<string, ErrorsState>;

                    try
                    {
                        var i = statesList.First(s => s.Value == ErrorsState.HaveUnseen);
                    }
                    catch (Exception)
                    {
                        parent.Background = Res.HaveSeenErrorsBrush;
                        return;
                    }
                    parent.Background = Res.ErrorBrush;
                });
        }


    }
    public enum ErrorsState
    {
        HaveUnseen,
        HaveSeen,
        NoErrors
    }
}
